﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ElevationViewSymbolNewUiManipulateStrategy.h"
#include "IElement.h"
#include "IModelView.h"
#include "IElementShapeHandle.h"
#include "IGenericElement.h"
#include "IElementBasicInformation.h"
#include "GbmpModelViewTypes.h"
#include "ElementUiManipulateStrategyRegister.h"
#include "ElevationViewSymbolNew.h"
#include "ElevationViewSymbolNewBehaviors.h"
#include "IElementPositionPoints.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

std::vector<ElementId> gcmp::ElevationViewSymbolNewUiManipulateStrategy::CreateAuxiliaryElements(gcmp::IModelView* pModelView, const IElement* pElement) const
{
    std::vector<ElementId> res;
    DBG_WARN_AND_RETURN_UNLESS(pElement != nullptr, res, L"invalid pElement",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_UNLESS(pModelView != nullptr, res, L"invalid pModelView",L"GDMPLab",L"2024-03-30");

    bool isSymbolVisible = pElement->GetViewSpecificShapeComponent() == nullptr ? true : pElement->GetViewSpecificShapeComponent()->IsVisibleInModelView(pModelView);
    if (!isSymbolVisible)
    {
        return res;
    }

    if (PlanViewType == pModelView->GetUserDataId())

    {
        //创建夹点
        res.push_back(CreateGripPoint(ElevationViewSymbolNewAuxiliaryType::HorizontalStart, pModelView->GetElementId(),pElement ));
        res.push_back(CreateGripPoint(ElevationViewSymbolNewAuxiliaryType::HorizontalEnd, pModelView->GetElementId(), pElement));

        //创建切割线（辅助线）
        res.push_back(CreateBorder(pModelView->GetElementId(), pElement));
    }
    return res;
}

ElementId gcmp::ElevationViewSymbolNewUiManipulateStrategy::CreateGripPoint(ElevationViewSymbolNewAuxiliaryType type, const gcmp::ElementId& modelviewId, const IElement* pElement) const
{
    const IGenericElement* pGenericElement = quick_cast<IGenericElement>(pElement);
    DBG_WARN_AND_RETURN_UNLESS(pGenericElement != nullptr, ElementId::InvalidID, L"invalid pGenericElement",L"GDMPLab",L"2024-03-30");

    const IElementPositionPoints* pOwnerPositionPoints = pGenericElement->GetPositionPoints();
    Vector3d pos = pOwnerPositionPoints ? pOwnerPositionPoints->GetControlPoint((int)type) : Vector3d::Zero;

    OwnerPtr<IElementShapeHandleBehavior> pGripPointBehavior = ElevationViewSymbolNewGripPointBehavior::Create(pElement->GetDocument(), modelviewId);

    IElementShapeHandle* pGripPoint = IElementShapeHandle::Create(
        TransferOwnership(pGripPointBehavior),
        pGenericElement->GetElementId(),
        pos, static_cast<int>(type));
    DBG_WARN_AND_RETURN_UNLESS(pGripPoint != nullptr, ElementId::InvalidID, L"invalid pGripPoint",L"GDMPLab",L"2024-03-30");

    return pGripPoint->GetElementId();
}


gcmp::ElementId gcmp::ElevationViewSymbolNewUiManipulateStrategy::CreateBorder(const gcmp::ElementId& modelviewId, const IElement* pElement) const
{
    const IGenericElement* pGenericElement = quick_cast<IGenericElement>(pElement);
    DBG_WARN_AND_RETURN_UNLESS(pGenericElement != nullptr, ElementId::InvalidID, L"invalid pGenericElement",L"GDMPLab",L"2024-03-30");

    //borderPos并不是必需数据，只是调用IElementShapeHandle::Create（）设置进去的，同时还为了后续调用UpdatePosition()以更新IElementShapeHandle。borderPos不是视点位置。
    Vector3d borderPos;

    OwnerPtr<IElementShapeHandleBehavior> pBorderBehavior = ElevationViewSymbolNewBorderBehavior::Create(pElement->GetDocument(), modelviewId);

    IElementShapeHandle* pBorder = IElementShapeHandle::Create(
        TransferOwnership(pBorderBehavior),
        pGenericElement->GetElementId(),
        borderPos, static_cast<int>(ElevationViewSymbolNewAuxiliaryType::Border));
    DBG_WARN_AND_RETURN_UNLESS(pBorder != nullptr, ElementId::InvalidID, L"invalid pBorder",L"GDMPLab",L"2024-03-30");

    return pBorder->GetElementId();

}

REGISTER_ELEMENT_UIMANIPULATE_STRATEGY(ElevationViewSymbolNew, ElevationViewSymbolNewUiManipulateStrategy);
